package com.ddwl.gate.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.annotation.Submit;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.schema.bo.gate.AutoRefundConfigBo;
import com.ddwl.schema.vo.gate.AutoRefundConfigVo;
import com.ddwl.gate.service.MobileRechargeAccountService;
import com.ddwl.gate.service.MobileRechargeConfigService;
import com.ddwl.gate.service.MobileRechargeRecordService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.gate.*;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.gate.MobileOperatorConfigVo;
import com.ddwl.schema.vo.gate.MobileRechargeConfigVo;
import com.ddwl.schema.vo.gate.MobileRechargeRecordVo;
import com.ddwl.schema.vo.order.StatisticsAmountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author hemiaojun
 * @Date 2020/9/22 14:54
 */
@Api(tags = "话费充值")
@RequestMapping("/mobile/cost")
@RestController
public class MobileCostController extends BaseController {

    @Resource
    private MobileRechargeConfigService mobileRechargeConfigService;
    @Resource
    private MobileRechargeRecordService mobileRechargeRecordService;
    @Resource
    private MobileRechargeAccountService mobileRechargeAccountService;

    @PostMapping("/config/add")
    @ApiOperation("新增话费充值配置信息")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo addConfig(@Valid  @RequestBody MobileRechargeConfigBo bo) {
        mobileRechargeConfigService.addConfig(bo);
        return RestVo.ok();
    }

    @PostMapping("/config/update")
    @ApiOperation("后台编辑话费充值配置")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo update(@Valid @RequestBody MobileRechargeConfigBo bo) {
        mobileRechargeConfigService.update(bo);
        return RestVo.ok();
    }

    @PostMapping("/config/delete")
    @ApiOperation("后台删除话费充值配置")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo delete(@RequestParam("id") String id) {
        mobileRechargeConfigService.delete(id);
        return RestVo.ok();
    }

    @PostMapping("/config/enableOrDisable")
    @ApiOperation("启用或禁用")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo enableOrDisable(@RequestBody MobileRechargeConfigBo bo) {
        mobileRechargeConfigService.enableOrDisable(bo);
        return RestVo.ok();
    }

    @PostMapping("/config/page")
    @ApiOperation("后台获取话费充值配置列表")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<PageVo<MobileRechargeConfigVo>> configPage(@RequestBody PageBo<MobileRechargeConfigPageBo> pageBo) {
        PageVo<MobileRechargeConfigVo> pageVo=mobileRechargeConfigService.page(pageBo);
        return RestVo.ok(pageVo);
    }

    @PostMapping("/check")
    @ApiOperation("检查公司账号情况")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.MALL})
    public RestVo<List<MobileRechargeConfigVo>> check() throws Exception{
        List<MobileRechargeConfigVo> list=mobileRechargeAccountService.check();
        return RestVo.ok(list);
    }

    @PostMapping("/config")
    @ApiOperation("app获取手机充值配置信息")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.MALL})
    public RestVo<MobileOperatorConfigVo> config(@RequestParam(value = "mobile",required = false) String mobile) {
        MobileOperatorConfigVo vo = mobileRechargeConfigService.getMobileOperatorConfigByMobile(mobile);
        return RestVo.ok(vo);
    }

    @PostMapping("/list")
    @ApiOperation("用户获取手机充值记录")
    @Auth(product  = {BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.MALL})
    public RestVo<PageVo<MobileRechargeRecordVo>> list(@RequestBody PageBo<MobileRechargeRecordPageBo> pageBo) {
        if (null == pageBo.getParam()) {
            pageBo.setParam(new MobileRechargeRecordPageBo());
        }
        pageBo.getParam().setUserId(getUserId());
        pageBo.getParam().setProduct(getProduct());
        PageVo<MobileRechargeRecordVo> pageVo=mobileRechargeRecordService.page(pageBo);
        return RestVo.ok(pageVo);
    }

    @PostMapping("/page")
    @ApiOperation("后台获取手机充值记录")
    @Auth(product  = {BaseEnum.ProductEnum.CONSOLE,BaseEnum.ProductEnum.API})
    public RestVo<PageVo<MobileRechargeRecordVo>> page(@RequestBody PageBo<MobileRechargeRecordPageBo> pageBo) {
        if (null == pageBo.getParam()) {
            pageBo.setParam(new MobileRechargeRecordPageBo());
        }
        pageBo.getParam().setProduct(getProduct());
        PageVo<MobileRechargeRecordVo> pageVo=mobileRechargeRecordService.page(pageBo);
        return RestVo.ok(pageVo);
    }

    @PostMapping("/amount")
    @ApiOperation("后台获取手机充值金额记录")
    @Auth(product  = {BaseEnum.ProductEnum.API})
    public RestVo<List<StatisticsAmountVo>> getAmount(@RequestBody MobileRechargeRecordPageBo pageBo) {
        List<StatisticsAmountVo> pageVo=mobileRechargeRecordService.getAmount(pageBo);
        return RestVo.ok(pageVo);
    }

    @PostMapping("/recharge")
    @ApiOperation("充值")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.MALL})
    public RestVo<MobileRechargeRecordVo> recharge(@Valid @RequestBody MobileRechargeRecordBo bo) {
        bo.setProduct(getAuthMap().get("pk"));
        bo.setUserId(getUserId());
        bo.setUserName(getAuthMap().get("nickname"));
        MobileRechargeRecordVo vo=mobileRechargeRecordService.recharge(bo);
        return RestVo.ok(vo);
    }

    @Submit
    @PostMapping("/pay")
    @ApiOperation("支付")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.MALL})
    public RestVo<String> buy(@Valid @RequestBody MobileRechargeBo bo) {
        fillBaseField(bo);
        return mobileRechargeRecordService.pay(bo);
    }

    @PostMapping("/status")
    @ApiOperation("查询订单状态")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.CONSOLE})
    public RestVo<MobileRechargeRecordVo> orderStatus(@RequestParam("id") String id) throws Exception{
        MobileRechargeRecordVo vo=mobileRechargeRecordService.orderStatus(id);
        return RestVo.ok(vo);
    }

    @Submit
    @GetMapping("/refund/{orderId}")
    @ApiOperation("退款")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<MobileRechargeRecordVo> refund(@PathVariable("orderId") String orderId) {
        RechargeRefundBo bo=new RechargeRefundBo();
        bo.setRechargeRecordId(orderId);
        bo.setOperatorId(getAuthMap().get("uid"));
        bo.setOperatorName(getAuthMap().get("nickname"));
        MobileRechargeRecordVo vo=mobileRechargeRecordService.refund(bo);
        return RestVo.ok(vo);
    }

    @PostMapping("/cancel")
    @ApiOperation("取消充值")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.MALL})
    public RestVo<MobileRechargeRecordVo> cancel(@RequestParam("id") String id){
        MobileRechargeRecordVo vo=mobileRechargeRecordService.cancel(id);
        return RestVo.ok(vo);
    }

    @PostMapping("/jdPhone")
    @ApiOperation("京东获取手机号运营商")
    public RestVo<String> jdPhone(@RequestParam String mobile) {
        String result=mobileRechargeRecordService.jdPhone(mobile);
        return RestVo.ok(result);
    }

}
